﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL = AccessDAL;

public partial class Control_ProductClassTreeView : System.Web.UI.UserControl
{
    DAL.ProductClassTableAdapters.ProductClassTableAdapter DPC =
        new DAL.ProductClassTableAdapters.ProductClassTableAdapter();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ProductClassTreeDataBind();
        }
    }


    protected void ProductClassTreeDataBind()
    {
        DataTable DT_ProductClassTreeView = DPC.GetDataOrderByClassCode(false);
        InitTree(TreeView1.Nodes, "0", DT_ProductClassTreeView);

    }

    private void InitTree(TreeNodeCollection tnParent, string parentId, DataTable dt)
    {
        foreach (DataRow dr in dt.Select("[PC_ParentID]='" + parentId + "'"))
        {
            TreeNode tn = new TreeNode();

            tn.Value = dr["PC_ID"].ToString();
            tn.Text = dr["PC_Title"].ToString();
            tn.Target = "_self";
            tn.NavigateUrl = "../Product.aspx?ClassID=" + dr["PC_ID"].ToString();

            tnParent.Add(tn);

            InitTree(tnParent[tnParent.Count - 1].ChildNodes, tn.Value, dt);
        }
    } 
}
